package com.irdstudio.efp.nls.api.rest;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import com.irdstudio.basic.framework.core.constant.PubConstant;
import com.irdstudio.basic.framework.core.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.irdstudio.efp.ctr.service.facade.CtrLoanContService;
import com.irdstudio.efp.ctr.service.vo.CtrLoanContVO;
import com.irdstudio.efp.e4a.service.facade.SRoleDataRuleService;
import com.irdstudio.efp.loan.service.facade.AccLoanService;
import com.irdstudio.efp.loan.service.vo.AccLoanVO;
import com.irdstudio.basic.framework.core.constant.DataRuleType;
import com.irdstudio.basic.framework.core.constant.ResponseData;
import com.irdstudio.basic.framework.web.controller.AbstractController;
import com.irdstudio.efp.nls.service.facade.NlsLoanApprovalInfoService;
import com.irdstudio.efp.nls.service.vo.NlsLoanApprovalInfoVO;

/**
 * 
 * 
 * @author wanglh
 * @since 
 * @version 1.0
 */

@RestController
@RequestMapping("/api")
public class NlsLoanApprovalInfoController extends AbstractController {

	@Autowired
	@Qualifier("nlsLoanApprovalInfoService")
	private NlsLoanApprovalInfoService nlsLoanApprovalInfoService;
	
	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;
	
	@Autowired
	@Qualifier("accLoanService")
	private AccLoanService accLoanService;
	
	@Autowired
	@Qualifier("ctrLoanContService")
	private CtrLoanContService ctrLoanContService;
	
	@PostMapping(value = "/nls/appr/infos")
	public @ResponseBody ResponseData<List<NlsLoanApprovalInfoVO>> querynlsLoanApprovalInfos(@RequestBody NlsLoanApprovalInfoVO vo) {
		/**
		 * 
		 * @date 2019-01-17
		 * @author wanglh
		 *  
		 * 【原代码】 List<NlsLoanApprovalInfoVO> outputVo = nlsLoanApprovalInfoService.querynlsLoanApprovalInfoByPage(vo);
		 * @modify qiuyf
		 * @since 2019年02月12日 下午4:41:15
		 */
		List<NlsLoanApprovalInfoVO> outputVo = null;
		// 根据用户角色获取用户权限方案
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);
		if (!StringUtil.isNullorBank(vo.getExportFlag()) && "exp".equals(vo.getExportFlag())) {
			logger.info("开始导出数据！");
			outputVo = nlsLoanApprovalInfoService.querynlsLoanApprovalInfo(vo, dataRuleType.getValue());
		} else {
			logger.info("开始查询数据！");
			outputVo = nlsLoanApprovalInfoService.querynlsLoanApprovalInfoByPage(vo, dataRuleType.getValue());
			appendEle(outputVo);
		}
		return getResponseData(outputVo);
	}

	private void appendEle(List<NlsLoanApprovalInfoVO> outputVo) {
		if (Objects.nonNull(outputVo)) {
			for (NlsLoanApprovalInfoVO item : outputVo) {
				AccLoanVO queryRsAccLoan = Optional.ofNullable(getEleFromLoan(item.getApplySeq())).orElseGet(AccLoanVO::new);
				CtrLoanContVO queryRsCtrLoanCont = Optional.ofNullable(getContNoFromCtr(item.getApplySeq())).orElseGet(CtrLoanContVO::new);
				
				item.setContNo(queryRsCtrLoanCont.getContNo());
				item.setBillNo(queryRsAccLoan.getBillNo());
				item.setRealityIrY(queryRsAccLoan.getRealityIrY());
				item.setRepaymentMode(queryRsAccLoan.getRepaymentMode());
			}
		}
	}

	private AccLoanVO getEleFromLoan(String applySeq) {
		logger.info("开始查询借据表，得到授信结束日期。授信协议号：" + applySeq);
		try {
			AccLoanVO accLoanVO = new AccLoanVO();
			accLoanVO.setApplySeq(applySeq);
			AccLoanVO queryRs = accLoanService.queryBRR(accLoanVO);
			if (Objects.nonNull(queryRs)) {
				return queryRs;
			} else {
				logger.info("查询结果为空！");
			}
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return null;
	}

	private CtrLoanContVO getContNoFromCtr(String applySeq) {
		logger.info("开始查询合同表，得到授信结束日期。授信协议号为：" + applySeq);
		try {
			CtrLoanContVO ctrLoanContVO = new CtrLoanContVO();
			ctrLoanContVO.setSerno(applySeq);
			CtrLoanContVO queryRs = ctrLoanContService.queryContNo(ctrLoanContVO);
			if (Objects.nonNull(queryRs)) {
				return queryRs;
			} else {
				logger.info("查询结果为空！");
			}
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return null;
	}
}
